package com.lv.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lv.pojo.Teacher;
import com.lv.service.TeacherService;
import com.lv.util.MD5;
import com.lv.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/4/28 19:05
 */
@Api(tags = "教师控制器")
@RestController
@RequestMapping("/sms/teacherController")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    @ApiOperation("查询教师，带分页模糊查询")
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result getTeachers(
            @ApiParam("分页数") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("页码大小") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("查询条件转换后端数据模型") Teacher teacher
    ){
        Page<Teacher> pageParam=new Page<>(pageNo,pageSize);
        //调用service层
        IPage<Teacher> IPage = teacherService.getTeacherByOpr(pageParam,teacher);
        return Result.ok(IPage);
    }


    @ApiOperation("添加或修改教师信息")
    @PostMapping("/saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(@RequestBody Teacher teacher){
        Integer id=teacher.getId();
        if(null == id || 0 == id){
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }
        teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }


    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacherByIds(@RequestBody List<Integer> ids){
        teacherService.removeByIds(ids);
        return Result.ok();
    }

}
